<?php
$section = 7;
include("includes/secure.php");
include("../includes/config.php");
include("../includes/class/school.class.php");
require_once("../includes/fckeditor/fckeditor_php5.php");
require_once("../includes/class/fileManagement.class.php");

$school = new School(); 
$file = new fileManagement();

$desc_es1 = new FCKeditor('descripcion');
	//$desc_es->ToolbarSet = "Basic";
	$desc_es1->ToolbarSet = "Default";
	$desc_es1->Height = "250";
	$desc_es1->Width = "750";
	
$codeSchool=$_GET['code'];
		
if(isset($_POST['add']) && $_POST['add'] == 1)
{
	$new_data->codeSchool	= $_POST['codeSchool'];
	$new_data->name 		= mysql_real_escape_string($_POST['name']);
	$new_data->address    	= mysql_real_escape_string($_POST['address']);
	$new_data->phone	    = mysql_real_escape_string($_POST['phone']);
	$new_data->description	= mysql_real_escape_string($_POST['descripcion']);
	
	//@unlink("file_upload/documentos/".$renombre);	
	if(isset($_FILES['archivo']))
	{
		for($i=0;$i<sizeof($_FILES['archivo']['tmp_name']);$i++)
		{
			if(is_uploaded_file($_FILES['archivo']['tmp_name'][$i]))
			{
				$documento = $file->subirArchivo($_FILES['archivo']['tmp_name'][$i],$_FILES['archivo']['name'][$i],'../file_upload/documentos/');								 
				$new_data->original 	= $_FILES['archivo']['name'][$i];
				$new_data->renam        = $documento;
				@unlink("../file_upload/documentos/".$_POST['esc_renam']);							
			} 		
		}
	}
	
	if($school->editSchool($new_data))
	{	
		$school->deleteMembers($new_data->codeSchool);
		$exito = true;
			
			$members =  explode(",",$_POST['members']);
			
			$charges  =  explode(",",$_POST['charges']);
			
			if($members[0]!='')
			{			
				for($i=0;$i<count($members);$i++)
				{	
					
					$dataMember->name 		= $members[$i];
					$dataMember->charges 	= $charges[$i];
					$dataMember->school 	= $new_data->codeSchool;
					
					if(!$school->addMember($dataMember))
					{
						$exito = false;
					}
				}
			}	
			if($exito)
			{
				echo "<script>alert('Colegio modificado con éxito')</script>";
				echo "<script>window.location.href='listSchools.php';</script>";	
			}
			else
			{
				echo "<script>alert('El Colegio no se pudo modificar')</script>";
				echo "<script>window.location.href='listSchools.php';</script>";	
			}	
		
	}
	else
	{
		echo "<script>alert('El Colegio no se pudo modificar')</script>";
		echo "<script>window.location.href='listSchools.php';</script>";	
	}
	
}
else
{

	$dataSchool = $school->getDataSchool($codeSchool);
	$desc_es1->Value = $dataSchool->description;
	$arreglo 	= $school->listEditMember($codeSchool); 
	
	$idMenber	= '';
	$idCharge	= '';
	$indice		= 0;
	$ind		= '';
	
	 $listMember= $school->getDataMember($codeSchool);
	 for($i=0;$i<count($listMember);$i++)
	 {
	 	$idMenber	.= "'".$listMember[$i]->nameMember."',";
		$idCharge	.= "'".$listMember[$i]->chargeMember."',";
		$ind		.= "'".$i."',";
	 }
	 $indice = $i;
	 
	 $idMenber = substr($idMenber,0,-1);	
	 $idCharge = substr($idCharge,0,-1);
	 $ind      = substr($ind,0,-1);	
	
		
}


?>
